package com.tywj.panda.customer.common.view.paging

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.annotation.DimenRes
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

import com.chad.library.adapter.base.BaseQuickAdapter
import com.tywj.panda.customer.common.R
import com.tywj.panda.customer.common.http.PDObjectCallback
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import com.tywj.panda.customer.common.widget.MyLoadMoreView
import com.tywj.panda.customer.common.widget.StatusLayout
import com.tywj.panda.customer.common.widget.TitleLayout
import com.tywj.lib.businessweak.entity.AbsPagingEntity
import com.tywj.lib.businessweak.paging.IPagingView
import com.tywj.lib.businessweak.paging.PagingHelper
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.net.retrofit.request.BaseRequest

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/05/12
 *     desc   : 分页加载数据Activity基类
 *     version: 1.0
 * </pre>
 */
abstract class BasePagingActivity<D : AbsPagingEntity<T>, T>(
    private val clazz: Class<D>
) : MyBaseActivity(), IPagingView<D, T> {

    /**
     *  RecyclerView适配器
     */
    protected val mAdapter: BaseQuickAdapter<T, *> by lazy { getRecyclerViewAdapter() }

    /**
     * 分页加载数据帮助类
     */
    protected lateinit var mPagingHelper: PagingHelper<D, T>

    protected val mTitleLayout by lazy { findTitleLayout() }
    protected val mSwipeRefreshLayout by lazy { findSwipeRefreshLayout() }
    protected val mStatusLayout by lazy { findStatusLayout() }
    protected val mRecyclerView by lazy { findRecyclerView() }

    override fun getLayoutID(): Int = R.layout.common_activity_base_paging

    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        mPagingHelper = PagingHelper(
            mSwipeRefreshLayout, mStatusLayout, mRecyclerView, mAdapter,
            MyLoadMoreView(), this, this
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        mSwipeRefreshLayout.setColorSchemeResources(R.color.common_yellow_FE)

        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
    }

    override fun initListener() {
        super.initListener()

        mStatusLayout.setOnEmptyLayoutButtonClick {
            // 重新加载数据
            mPagingHelper.refresh()
        }

        mStatusLayout.setOnErrorLayoutButtonClick {
            // 重新加载数据
            mPagingHelper.refresh()
        }
    }

    override fun loadDataOnCreate() {
        super.loadDataOnCreate()

        mPagingHelper.refresh()
    }

    override fun executeRequest(
        request: BaseRequest<*>,
        successBlock: (D) -> Unit,
        errorBlock: (code: Int, message: String) -> Unit,
        completeBlock: () -> Unit
    ) {
        request.execute(object : PDObjectCallback<D>(clazz) {
            override fun success(data: D) {
                successBlock.invoke(data)
            }

            override fun error(code: Int, message: String) {
                super.error(code, message)

                errorBlock.invoke(code, message)
            }

            override fun onComplete() {
                super.onComplete()

                completeBlock.invoke()
            }
        })
    }

    protected open fun findTitleLayout(): TitleLayout = findViewById(R.id.titleLayout)

    protected open fun findSwipeRefreshLayout(): SwipeRefreshLayout {
        return findViewById(R.id.swipeRefreshLayout)
    }

    protected open fun findStatusLayout(): StatusLayout = findViewById(R.id.statusLayout)

    protected open fun findRecyclerView(): RecyclerView = findViewById(R.id.recyclerView)



    /**
     * 增加分割线
     */
    protected fun addItemDecoration(@DimenRes resId: Int) {
        mRecyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                val position = parent.getChildLayoutPosition(view)
                if (position != RecyclerView.NO_POSITION) {
                    when (position) {
                        // 第一个
                        0 -> outRect.top = 0
                        // footer
                        mAdapter.data.size + mAdapter.headerLayoutCount -> outRect.top = 0
                        // 其他
                        else -> outRect.top = ResourcesUtil.getDimensionPixelSize(resId)
                    }
                }
            }
        })
    }
}